/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.swinvehicle.managedbean;

import au.edu.swin.swinvehicle.bookingtype.BookingTypeManagerException;
import au.edu.swin.swinvehicle.bookingtype.BookingTypeManagerRemote;
import au.edu.swin.swinvehicle.bookingtype.BookingTypeTO;
import au.edu.swin.swinvehicle.corporateclient.CorporateClientTO;
import au.edu.swin.swinvehicle.driver.DriverManagerException;
import au.edu.swin.swinvehicle.driver.DriverManagerRemote;
import au.edu.swin.swinvehicle.driver.DriverTO;
import au.edu.swin.swinvehicle.eventtype.EventTypeManagerException;
import au.edu.swin.swinvehicle.eventtype.EventTypeManagerRemote;
import au.edu.swin.swinvehicle.eventtype.EventTypeTO;
import au.edu.swin.swinvehicle.vehicle.VehicleManagerException;
import au.edu.swin.swinvehicle.vehicle.VehicleManagerRemote;
import au.edu.swin.swinvehicle.vehicle.VehicleTO;
import au.edu.swin.swinvehicle.vehiclebooking.VehicleBookingDetailedTO;
import au.edu.swin.swinvehicle.vehiclebooking.VehicleBookingManagerException;
import au.edu.swin.swinvehicle.vehiclebooking.VehicleBookingManagerRemote;
import au.edu.swin.swinvehicle.vehicletype.VehicleTypeManagerException;
import au.edu.swin.swinvehicle.vehicletype.VehicleTypeManagerRemote;
import au.edu.swin.swinvehicle.vehicletype.VehicleTypeTO;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author Administrator
 */
public class VehicleBookingJSFManagedBean {

    public static final String LIST_VEHICLEBOOKINGS = "listVehicleBookings";
    public static final String ADD_VEHICLEBOOKING = "addVehicleBooking";
    public static final String EDIT_VEHICLEBOOKING = "editVehicleBooking";
    public static final String ASSIGN_VEHICLEBOOKING = "assignVehicleBooking";
    @EJB
    private VehicleBookingManagerRemote vehicleBookingManagerBean;
    @EJB
    private BookingTypeManagerRemote bookingTypeManagerBean;
    @EJB
    private EventTypeManagerRemote eventTypeManagerBean;
    @EJB
    private VehicleTypeManagerRemote vehicleTypeManagerBean;
    @EJB
    private VehicleManagerRemote vehicleManagerBean;
    @EJB
    private DriverManagerRemote driverManagerBean;
    private VehicleBookingDetailedTO vehicleBooking;

    /** Creates a new instance of VehicleBookingJSFManagedBean */
    public VehicleBookingJSFManagedBean() {
        this.vehicleBooking = new VehicleBookingDetailedTO();
        this.vehicleBooking.setVehicleType(new VehicleTypeTO());
        this.vehicleBooking.setBookingType(new BookingTypeTO());
        this.vehicleBooking.setEventType(new EventTypeTO());
        this.vehicleBooking.setCorporateClient(new CorporateClientTO());
        this.vehicleBooking.setDriver(new DriverTO());
        this.vehicleBooking.setVehicle(new VehicleTO());
    }

    public VehicleBookingDetailedTO getVehicleBooking() {
        return vehicleBooking;
    }

    public void setVehicleBooking(VehicleBookingDetailedTO vehicleBooking) {
        this.vehicleBooking = vehicleBooking;
    }

    public String addVehicleBooking() {
        try {
            this.vehicleBookingManagerBean.addVehicleBooking(this.vehicleBooking);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_INFO);
            msg.setSummary("Added:");
            msg.setDetail("The new vehicle booking has been successfully added");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleBookingSuccessNotification", msg);

            return LIST_VEHICLEBOOKINGS;
        } catch (VehicleBookingManagerException ex) {
            Logger.getLogger(VehicleBookingJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot add new vehicle booking. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleBookingErrorNotification", msg);

            return ADD_VEHICLEBOOKING;
        }
    }

    public List<VehicleBookingDetailedTO> getVehicleBookings() {
        List<VehicleBookingDetailedTO> vehicleBookings = new ArrayList<VehicleBookingDetailedTO>();
        try {
            vehicleBookings = this.vehicleBookingManagerBean.getAllDetailedVehicleBookings();
        } catch (VehicleBookingManagerException ex) {
            Logger.getLogger(VehicleBookingJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return vehicleBookings;
    }

    public List<SelectItem> getVehicleTypes() {
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        selectItems.add(new SelectItem(null, "--- Select vehicle type ---"));
        try {
            List<VehicleTypeTO> vehicleTypes = this.vehicleTypeManagerBean.getAllVehicleTypes();
            for (VehicleTypeTO vehicleType : vehicleTypes) {
                selectItems.add(new SelectItem(vehicleType.getId(), vehicleType.getCode()));
            }
        } catch (VehicleTypeManagerException ex) {
            Logger.getLogger(VehicleBookingJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return selectItems;
    }

    public List<SelectItem> getBookingTypes() {
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        selectItems.add(new SelectItem(null, "--- Select booking type ---"));
        try {
            List<BookingTypeTO> bookingTypes = this.bookingTypeManagerBean.getAllBookingTypes();
            for (BookingTypeTO bookingType : bookingTypes) {
                selectItems.add(new SelectItem(bookingType.getId(), bookingType.getCode()));
            }
        } catch (BookingTypeManagerException ex) {
            Logger.getLogger(VehicleBookingJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return selectItems;
    }

    public List<SelectItem> getEventTypes() {
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        selectItems.add(new SelectItem(null, "--- Select event type ---"));
        try {
            List<EventTypeTO> eventTypes = this.eventTypeManagerBean.getAllEventTypes();
            for (EventTypeTO eventType : eventTypes) {
                selectItems.add(new SelectItem(eventType.getId(), eventType.getCode()));
            }
        } catch (EventTypeManagerException ex) {
            Logger.getLogger(VehicleBookingJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return selectItems;
    }

    public List<SelectItem> getAvailableVehicles() {
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        selectItems.add(new SelectItem(null, "--- Select vehicle ---"));
        try {
            List<VehicleTO> vehicles = this.vehicleManagerBean.getAllVehicles();
            for (VehicleTO vehicle : vehicles) {
                selectItems.add(new SelectItem(vehicle.getId(), vehicle.getName()));
            }
        } catch (VehicleManagerException ex) {
            Logger.getLogger(JobReportJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        // TODO: Find all available vehicles
        return selectItems;
    }

    public List<SelectItem> getAvailableDrivers() {
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        selectItems.add(new SelectItem(null, "--- Select driver ---"));
        try {
            List<DriverTO> drivers = this.driverManagerBean.getAllDrivers();
            for (DriverTO driver : drivers) {
                selectItems.add(new SelectItem(driver.getId(), driver.getUsername()));
            }
        } catch (DriverManagerException ex) {
            Logger.getLogger(JobReportJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        // TODO: Find all available vehicles
        return selectItems;
    }

    public String editVehicleBooking() {
        String vehicleBookingIdString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("vehicleBookingId");
        try {
            Long vehicleBookingId = Long.parseLong(vehicleBookingIdString);
            this.vehicleBooking = this.vehicleBookingManagerBean.getVehicleBooking(vehicleBookingId);
            if (this.vehicleBooking == null) {
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                msg.setSummary("Invalid vehicle booking ID:");
                msg.setDetail("The vehicle booking does not exist. Please try again.");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleBookingErrorNotification", msg);

                return LIST_VEHICLEBOOKINGS;
            } else {
                return EDIT_VEHICLEBOOKING;
            }
        } catch (VehicleBookingManagerException ex) {
            Logger.getLogger(VehicleBookingJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot edit vehicle booking. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleBookingErrorNotification", msg);

            return LIST_VEHICLEBOOKINGS;
        }
    }

    public String saveVehicleBooking() {
        try {
            this.vehicleBookingManagerBean.editVehicleBooking(this.vehicleBooking);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_INFO);
            msg.setSummary("Updated:");
            msg.setDetail("The vehicle booking has been successfully updated");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleBookingSuccessNotification", msg);

            return LIST_VEHICLEBOOKINGS;
        } catch (VehicleBookingManagerException ex) {
            Logger.getLogger(VehicleBookingJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot edit vehicle booking. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleBookingErrorNotification", msg);

            return EDIT_VEHICLEBOOKING;
        }
    }

    public String assignVehicleBooking() {
        String vehicleBookingIdString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("vehicleBookingId");
        try {
            Long vehicleBookingId = Long.parseLong(vehicleBookingIdString);
            this.vehicleBooking = this.vehicleBookingManagerBean.getVehicleBooking(vehicleBookingId);
            if (this.vehicleBooking == null) {
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                msg.setSummary("Invalid vehicle booking ID:");
                msg.setDetail("The vehicle booking does not exist. Please try again.");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleBookingErrorNotification", msg);

                return LIST_VEHICLEBOOKINGS;
            } else {
                return ASSIGN_VEHICLEBOOKING;
            }
        } catch (VehicleBookingManagerException ex) {
            Logger.getLogger(VehicleBookingJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot assign vehicle booking. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleBookingErrorNotification", msg);

            return LIST_VEHICLEBOOKINGS;
        }
    }

    public String saveAssignVehicleBooking() {
        try {
            this.vehicleBookingManagerBean.assignVehicleBooking(this.vehicleBooking);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_INFO);
            msg.setSummary("Assigned:");
            msg.setDetail("The vehicle booking has been successfully assigned");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleBookingSuccessNotification", msg);

            return LIST_VEHICLEBOOKINGS;
        } catch (VehicleBookingManagerException ex) {
            Logger.getLogger(VehicleBookingJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot assign vehicle booking. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleBookingErrorNotification", msg);

            return ASSIGN_VEHICLEBOOKING;
        }
    }

    public String deleteVehicleBooking() {
        String vehicleBookingIdString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("vehicleBookingId");
        try {
            Long vehicleBookingId = Long.parseLong(vehicleBookingIdString);
            this.vehicleBookingManagerBean.deleteVehicleBooking(vehicleBookingId);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_INFO);
            msg.setSummary("Deleted:");
            msg.setDetail("The vehicle booking has been successfully deleted");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleBookingSuccessNotification", msg);

            return LIST_VEHICLEBOOKINGS;
        } catch (VehicleBookingManagerException ex) {
            Logger.getLogger(VehicleBookingJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot delete vehicle booking. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralVehicleBookingErrorNotification", msg);

            return LIST_VEHICLEBOOKINGS;
        }
    }
}
